Searching for a key in a multi dimensional array and adding it to another array [migrated]

Posted by Moha on Programmers See other posts from Programmers or by Moha
Published on 2012-10-14T14:52:37Z Indexed on 2012/10/14 15:49 UTC
Read the original article Hit count: 331

Filed under:
|
|

Let's say I have two multi dimensional arrays:

array1 (
    stuff1 = array (
        data = 'abc'
    )
    stuff2 = array (
        something = '123'
        data = 'def'
    )
    stuff3 = array (
        stuff4 = array (
            data = 'ghi'
        )
    )
)

array2 (
   stuff1 = array (
   )
   stuff3 = array (
       anything = '456'
   )
)

What I want is to search the key 'data' in array1 and then insert the key and value to array2 regardless of the depth. So wherever key 'data' exists in array1 it gets added to array2 with the exact depth (and key names) as in array1 AND without modifying any other keys.

How can I do this recursively?

© Programmers or respective owner

Related posts about php

Related posts about recursion